﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VOL.Core.Extensions;
using VOL.Entity.DomainModels.WorkforceManage;
using VOL.SmartMecial.Dtos;
using VOL.SmartMecial.Infrastructure;
using VOL.SmartMecial.IServices.WorkforceManage;

namespace VOL.SmartMecial.Services.WorkforceManage
{
    public class ReplaceWorkShow : IReplaceWorkShow
    {
        private readonly IBaseRepository<ReplaceTableModel> _replaceTableRep;
        private readonly IBaseRepository<WorkforceModel> _workforceRep;
        private readonly IBaseRepository<ReplaceWorkModel> _replaceWorkRep;

        public ReplaceWorkShow(IBaseRepository<ReplaceTableModel> replaceTableRep, IBaseRepository<WorkforceModel> workforceRep, IBaseRepository<ReplaceWorkModel> replaceWorkRep)
        {
            _replaceTableRep = replaceTableRep;
            _workforceRep = workforceRep;
            _replaceWorkRep = replaceWorkRep;
        }
        public async Task<List<ReplaceWorkDTO>> ReplaceWorkShows()
        {

            var replaceWork = from a in _replaceWorkRep.GetAll()
                              join b in _workforceRep.GetAll() on a.NurseCode equals b.NurseCode
                              join c in _replaceTableRep.GetAll() on a.ReplaceCode equals c.ReplaceCode
                              select new ReplaceWorkDTO
                              {
                                  Id = a.Id,
                                  ApplicantType = a.ApplicantType,
                                  ReplaceCode = a.ReplaceCode,
                                  AuditComment = a.AuditComment,
                                  AuditorCode = a.AuditorCode,
                                  AuditState = a.AuditState,
                                  EndTime = a.EndTime,
                                  isDel = a.isDel,
                                  NurseCode = a.NurseCode,
                                  NurseName = b.NurseName,
                                  Reason = a.Reason,
                                  ReplaceName = c.ReplaceName,
                                  StartTime = a.StartTime,

                              };
            return await replaceWork.Distinct().ToListAsync();


        }
        /// <summary>
        /// 修改审批状态
        /// </summary>
        /// <param name="auditState">审批状态</param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<object> UpdateAuditState(int id,string auditState,string auditComment)
        {
            var approval =await _replaceWorkRep.FindAsync(id);
            if (approval != null)
            {
                //设置新的审批状态
                approval.AuditState =auditState;
                approval.AuditComment = auditComment;

            }
            await _replaceWorkRep.UpdateAsync(approval);
            return Task.FromResult(approval);
        }
    }
}
